package com.hsd.controller;

import com.hsd.entity.Log;
import com.hsd.service.IAccountService;
import com.hsd.service.ILogService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/account")
public class APIController extends BaseController {
    @Resource
    private IAccountService accountService;
    @Resource
    private ILogService logService;

    /**
     * 获取所有用户账号信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public Map<String, Object> findAllAccount() {
        return response(accountService.findAll());
    }

    /**
     * 存钱入口
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map<String, Object> saveMoney(String acc, Double money) {
        boolean save = accountService.save(acc, money);
        return response(save);
    }

    /**
     * 取钱入口
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/take", method = RequestMethod.POST)
    public Map<String, Object> takeMoney(String acc, Double money) {
        boolean take = accountService.take(acc, money);
        return response(take);
    }

    /**
     * 转账入口
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/transfer", method = RequestMethod.POST)
    public Map<String, Object> transferMoney(String f_acc, String t_acc, Double money) {
        System.out.println(f_acc+":"+t_acc+":"+money);
        boolean take = accountService.transfer(f_acc, t_acc, money);
        return response(take);
    }

    /**
     * 账户日志入口
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/log", method = RequestMethod.GET)
    public Map<String, Object> logOperation(String acc) {
        return response(logService.findByAcc(acc));
    }


}
